﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using cecbData;
using System.Windows.Media.Animation;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;
using cecbClasses.Data_Adapters;

namespace CECB
{
    /// <summary>
    /// Interaction logic for FillMatrix_Window.xaml
    /// </summary>
    public partial class FillMatrix_Window : Window
    {
        bool IsProjectSelected = false;
        bool IsEmailedDetails = false;

        public FillMatrix_Window()
        {
            InitializeComponent();
            Dss.Methods.FillMatrix pq = new Dss.Methods.FillMatrix();
            ProjectAutocompleteTextBox.ItemsSource = new Func_Projects().getAllProjects();

            comboBoxComponents.ItemsSource = new Func_Components().getComponents();
            ModelList = new ObservableCollection<addLeoValuews>();
            DataContext = this;
            clearTwoValues();
             
        }

        List<cecbClasses.Data_LeopoldMatrixValues> lstValues;
        List<cecbClasses.Data_Activities> lstActivity;
        List<cecbClasses.Data_Impacts> lstImpacts;
        String projectRef = "";

        public FillMatrix_Window(List<cecbClasses.Data_LeopoldMatrixValues> listval, String projref, List<cecbClasses.Data_Activities> listAct, List<cecbClasses.Data_Impacts> listImp)
        {
            InitializeComponent();
          //  ProjectAutocompleteTextBox.ItemsSource = new Func_Projects().getAllProjects();
            ProjectAutocompleteTextBox.Text = projref;
            comboBoxComponents.ItemsSource = new Func_Components().getComponents();

            ModelList = new ObservableCollection<addLeoValuews>();
            DataContext = this;
            clearTwoValues();

            lstValues = listval;
            projectRef = projref;
            lstImpacts = listImp;
            lstActivity = listAct;

            IsEmailedDetails = true;
          //  MessageBox.Show(lstValues.Count + "ss  " + listAct.Count + "dddf " + listImp.Count);
        }

        


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
           
        }

        private void textBoxImpactSearch_TextChanged(object sender, TextChangedEventArgs e)
        {



            if (!IsEmailedDetails)
            {

                if (IsProjectSelected)
                {
                    Dss.Methods.FillMatrix fillimp = new Dss.Methods.FillMatrix();
                    listBoxImpacts.ItemsSource = new Func_Impacts().getImpactsFromProjectImpacts(ProjectAutocompleteTextBox.Text.ToString(), textBoxImpactSearch.Text.Trim());
                    (FindResource("drop1") as Storyboard).Begin(this);
                }
                else
                {
                    textBoxImpactSearch.Clear();

                }

            }
            else
            {
                listBoxImpacts.ItemsSource = lstImpacts;
                (FindResource("drop1") as Storyboard).Begin(this);
            }

        }

        private void textBoxActivitySearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (!IsEmailedDetails)
            {
                if (IsProjectSelected)
                {

                    listBoxActivities.ItemsSource = new Func_Activities().getActivityFromProjectActivity(ProjectAutocompleteTextBox.Text.ToString(), textBoxActivitySearch.Text.Trim());
                    (FindResource("drop2") as Storyboard).Begin(this);
                }
                else
                {
                    // MessageBox.Show("Select a project", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
                    textBoxActivitySearch.Clear();
                }

            }
            else
            {

                listBoxActivities.ItemsSource = lstActivity;
                (FindResource("drop2") as Storyboard).Begin(this);
            }
        }

        private void buttonImpactUp_Click(object sender, RoutedEventArgs e)
        {
            
            if(listBoxImpacts.SelectedItems.Count != 0)
            {
                int selectedIndex = listBoxImpacts.SelectedIndex;
                selectedIndex--;

                Impacts imp;
                imp = (Impacts)listBoxImpacts.Items[selectedIndex];
                String x = imp.impt_name;
                String xid = imp.impt_reference;
                labelImpact.Content = x;
            }

        }

        private void buttonImpactdown_Click(object sender, RoutedEventArgs e)
        {
            if (listBoxImpacts.SelectedItems.Count != 0)
            {
                int selectedIndex = listBoxImpacts.SelectedIndex;
                selectedIndex++;

                Impacts imp;
                imp = (Impacts)listBoxImpacts.Items[selectedIndex];
                String x = imp.impt_name;
                String xid = imp.impt_reference;
                labelImpact.Content = x;
                listBoxImpacts.SelectedItem = selectedIndex;
            }
        }

        private void buttonActivityUp_Click(object sender, RoutedEventArgs e)
        {
            if (listBoxActivities.SelectedItems.Count != 0)
            {
                clearTwoValues();
                int selectedIndex = listBoxActivities.SelectedIndex;
                selectedIndex--;
                stackPanelActivity.Background = new SolidColorBrush(Colors.Red);
                Activities imp;
                imp = (Activities)listBoxActivities.Items[selectedIndex];
                String x = imp.actv_name;
                String xid = imp.actv_reference;
                labelActivity.Content = x;
            }
        }

        private void buttonActivitydown_Click(object sender, RoutedEventArgs e)
        {

        }

        private void listBoxImpacts_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            if (!IsEmailedDetails)
            {

                if (listBoxImpacts.SelectedIndex != -1)
                {
                    try
                    {
                        buttonConfirm.IsEnabled = true;
                        clearTwoValues();
                        stackPanelImpact.Background = new SolidColorBrush(Colors.Red);
                        Impacts name = (Impacts)listBoxImpacts.SelectedItem;
                        String x = name.impt_name;
                        _impactRef = name.impt_reference;

                        labelImpact.Content = x;

                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("System Error" + ex.Message, " Error Message", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
            }
            else {

                buttonConfirm.IsEnabled = true;
                clearTwoValues();
                stackPanelImpact.Background = new SolidColorBrush(Colors.Red);
                cecbClasses.Data_Impacts name = (cecbClasses.Data_Impacts)listBoxImpacts.SelectedItem;
                String x = name.Impt_name.ToString();
                labelImpact.Content = x;
            }
           



         
        }
        String _activityRef = null;
        String _impactRef = null;

        private void listBoxActivities_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // labelActivity.Content = listBoxActivities.SelectedValue.ToString();
            if (!IsEmailedDetails)
            {

                if (listBoxActivities.SelectedIndex != -1)
                {
                    try
                    {
                        buttonConfirm.IsEnabled = true;
                        stackPanelActivity.Background = new SolidColorBrush(Colors.Red);
                        Activities name = (Activities)listBoxActivities.SelectedItem;
                        String x = name.actv_name;
                        _activityRef = name.actv_reference;

                        labelActivity.Content = x;

                    }
                    catch (Exception)
                    {

                    }
                }
            }
            else {

                buttonConfirm.IsEnabled = true;
                stackPanelActivity.Background = new SolidColorBrush(Colors.Red);
                cecbClasses.Data_Activities name = (cecbClasses.Data_Activities)listBoxActivities.SelectedItem;
                String x = name.actv_name;
             //   _activityRef = name.actv_reference;

                labelActivity.Content = x;

            
            }


        //    DssEntities dataContext = new DssEntities();
        //    String y = dataContext.Components.First(i => i.cmpt_reference == x).cmpt_name.ToString();
        }
        Projects proj;
        private void ProjectTextBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (ComboboxSelectMatrix.SelectedIndex > -1)
                {
                    IsProjectSelected = true;

                    proj = (Projects)ProjectAutocompleteTextBox.SelectedItem;
                    labelProject.Content = ProjectAutocompleteTextBox.Text.ToString() + "  Id =>  " + proj.proj_reference;
                }
                else
                {
                    ProjectAutocompleteTextBox.Text = "";
                    
                    MessageBox.Show("Select a matrix type!", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            catch (Exception)
            {


            }
           //Dss.Methods.FillMatrix fillact = new Dss.Methods.FillMatrix();
         //   ProjectAutocompleteTextBox.ItemsSource = fillact.getProjectsByName(ProjectAutocompleteTextBox.Text.Trim());
        }

        private void ProjectAutocompleteTextBox_TextChanged(object sender, RoutedEventArgs e)
        {
            IsProjectSelected = false;
            labelProject.Content = "";
        }

        private void buttonConfirm_Click(object sender, RoutedEventArgs e)
        {
            if (IsProjectSelected)
            {
                if (labelImpact.Content.ToString() == "")
                {
                    MessageBox.Show("Enter impact name", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else if (labelActivity.Content.ToString() == "")
                {
                    MessageBox.Show("Enter activity name", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else
                {
                   stackPanelActivity.Background = new SolidColorBrush(Colors.Green);
                   stackPanelImpact.Background = new SolidColorBrush(Colors.Green);
                   textBoxImportance.IsEnabled = true;
                   textBoxMagnitude.IsEnabled = true;
                }
            }
            else
                MessageBox.Show("Select a project", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
        }

        private void buttonAddMatrix_Click(object sender, RoutedEventArgs e)
        {
            String impName = labelImpact.Content.ToString();
            String actName = labelActivity.Content.ToString();
            String ival    = textBoxMagnitude.Text.ToString();
            String mval    = textBoxImportance.Text.ToString();

            if (labelImpact.Content.ToString() == "")
            {
                MessageBox.Show("Enter impact name", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else if (labelActivity.Content.ToString() == "")
            {
                MessageBox.Show("Enter activity name", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            //else if (textBoxMagnitude.Text.ToString() == "")
            //{
            //    if (ComboboxSelectMatrix.SelectedIndex == 1)
            //    MessageBox.Show("Enter magnitude value", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
            //}
            //else if (textBoxImportance.Text.ToString() == "")
            //{
               
            //    MessageBox.Show("Enter importens value", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
            //}
            else if(true)
            {
                if (ComboboxSelectMatrix.SelectedIndex == 1) {

                    Dss.Methods.FillMatrix fillmx = new Dss.Methods.FillMatrix();
                    if (fillmx.InsertValues(_impactRef, _activityRef, proj.proj_reference.ToString(), textBoxMagnitude.Text, textBoxImportance.Text))
                    {
                        MessageBox.Show("Inserted Leopold Matrix Value", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                        model = new addLeoValuews();
                        model.impactName = impName;
                        model.activityName = actName;
                        model.magVal = ival;
                        model.impVal = mval;
                        ModelList.Add(model);
                        buttonConfirm.IsEnabled = false;
                    }
                    else
                    {

                        MessageBoxResult result = MessageBox.Show("Values are already in database! Do you want to update?", "Message", MessageBoxButton.YesNo, MessageBoxImage.Question);
                        if (result == MessageBoxResult.Yes)
                        {
                            // fillmx.UpdateValues(_impactRef, _activityRef, proj.proj_reference.ToString(), textBoxMagnitude.Text, textBoxImportance.Text);
                        }

                    }
                }

                else if (ComboboxSelectMatrix.SelectedIndex == 0) {


                    if (new cecbClasses.Data_Adapters.SimpleMatrixCalculation().InsertValuesToSimpleMatrix(_impactRef, _activityRef, proj.proj_reference.ToString(), textBoxImportance.Text))
                    {
                        MessageBox.Show("Inserted Simple Matrix Value", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                        model = new addLeoValuews();
                        model.impactName = impName;
                        model.activityName = actName;
                        model.magVal = "   ---";
                        model.impVal = textBoxImportance.Text;
                        ModelList.Add(model);
                        buttonConfirm.IsEnabled = false;
                    }
                    else
                    {

                        MessageBoxResult result = MessageBox.Show("Values are already in database! Do you want to update?", "Message", MessageBoxButton.YesNo, MessageBoxImage.Question);
                        if (result == MessageBoxResult.Yes)
                        {
                            // fillmx.UpdateValues(_impactRef, _activityRef, proj.proj_reference.ToString(), textBoxMagnitude.Text, textBoxImportance.Text);
                        }

                    }
                
                }

            textBoxImportance.Text = "";
            textBoxMagnitude.Text = "";
            textBoxImportance.IsEnabled = false;
            textBoxMagnitude.IsEnabled = false;
            stackPanelActivity.Background = new SolidColorBrush(Colors.Red);
            stackPanelImpact.Background = new SolidColorBrush(Colors.Red);
             
            }
        }
        addLeoValuews model;
        private List<cecbClasses.Data_LeopoldMatrixValues> list;
        private List<cecbClasses.Data_LeopoldMatrixValues> lstnew;
        public ObservableCollection<addLeoValuews> ModelList { get; set; }

        private void clearTwoValues() 
        {
            textBoxMagnitude.Clear();
            textBoxImportance.Clear();
            textBoxImportance.IsEnabled = false;
            textBoxMagnitude.IsEnabled = false;
            stackPanelActivity.Background = new SolidColorBrush(Colors.Transparent);
            stackPanelImpact.Background = new SolidColorBrush(Colors.Transparent);
            labelActivity.Content = "";
            labelImpact.Content = "";
        }

        private void textBoxMagnitude_TextChanged(object sender, TextChangedEventArgs e)
        {

            //Regex regex = new Regex("[^0-9]+");
            //e.Handled = regex.IsMatch(e.Text);

            //if (textBox_ContactNo.Text.Length == 10)
            //    e.Handled = true;
        }

        private void textBoxMagnitude_KeyDown(object sender, KeyEventArgs e)
        {
           
        }

        private void textBoxMagnitude_PreviewKeyDown(object sender, KeyEventArgs e)
        {

        }

        private void textBoxMagnitude_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
             Regex regex = new Regex("[^0-9]+");
            e.Handled = regex.IsMatch(e.Text);
        }

        private void textBoxImportance_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            Regex regex = new Regex("[^0-9]+");
            e.Handled = regex.IsMatch(e.Text);
        }

        private void textBoxImpactSearch_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (!IsProjectSelected)
            {
                MessageBox.Show("Select A Project", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
                 e.Handled = true;

            }
            else
                if (comboBoxMainImpactCat.SelectedIndex == -1)
                {
                    MessageBox.Show("Select Bord Category", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
                   e.Handled = true;
                }
                else if (comboBoxComponents.SelectedIndex == -1)
                {
                    MessageBox.Show("Select Sub Component", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
                     e.Handled = true;
                }
                else
                    e.Handled = false;

        }

        private void textBoxActivitySearch_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (!IsProjectSelected)
            {
                MessageBox.Show("Select A Project", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
                e.Handled = true;

            }
            else
                if (comboBoxMainActivityCat.SelectedIndex == -1)
                {
                    MessageBox.Show("Select Activity Phase", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
                    e.Handled = true;
                }
                else
                    e.Handled = false;
        }

        

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void ComboboxSelectMatrix_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ComboboxSelectMatrix.SelectedIndex == 0) {
                lblimp.Visibility = System.Windows.Visibility.Hidden;
                lblmag.Visibility = System.Windows.Visibility.Visible;
                textBoxMagnitude.Visibility = System.Windows.Visibility.Hidden;
                lblmag.Content = "Importance Value";
                 
            }
            else if (ComboboxSelectMatrix.SelectedIndex == 1) {

                lblimp.Visibility = System.Windows.Visibility.Visible;
                lblmag.Visibility = System.Windows.Visibility.Visible;
                textBoxImportance.Visibility = System.Windows.Visibility.Visible;
                textBoxMagnitude.Visibility = System.Windows.Visibility.Visible;
                lblmag.Content = "Magnitude Value";
            }
        }


    }

    

    public class addLeoValuews 
    {
        public addLeoValuews()
        {

        }
         
        private String _impactName;

        public String impactName
        {
            get { return _impactName; }
            set { _impactName = value; }
        }

        private String _activityName;

        public String activityName 
        {
            get { return _activityName; }
            set { _activityName = value; }
        }

        private String _magVal;

        public String magVal
        {
            get { return _magVal; }
            set { _magVal = value; }
        }

        private String _impVal;

        public String impVal
        {
            get { return _impVal; }
            set { _impVal = value; }
        }
        
        
        
        
    
    }


    
}
